package cn.eslm.flash.api.controller.cms;

import cn.eslm.flash.api.controller.BaseController;
import cn.eslm.flash.bean.constant.factory.PageFactory;
import cn.eslm.flash.bean.entity.cms.Dish;
import cn.eslm.flash.bean.entity.cms.Order;
import cn.eslm.flash.bean.enumeration.Permission;
import cn.eslm.flash.bean.vo.front.Rets;
import cn.eslm.flash.service.cms.DishService;
import cn.eslm.flash.service.cms.OrderService;
import cn.eslm.flash.utils.DateUtil;
import cn.eslm.flash.utils.factory.Page;
import cn.eslm.flash.bean.vo.query.SearchFilter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.nutz.json.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private DishService dishService;
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @RequiresPermissions(value = {Permission.ORDER})
    public Object list(@RequestParam(required = false) String userID,
                       @RequestParam(required = false) String mobile,
                       @RequestParam(required = false) String startDate,
                       @RequestParam(required = false) String endDate,
                       @RequestParam(required = false) Integer status

    ) {
        Page<Order> page = new PageFactory<Order>().defaultPage();
        page.addFilter("createTime", SearchFilter.Operator.GTE, DateUtil.parse(startDate,"yyyyMMddHHmmss"));
        page.addFilter("createTime", SearchFilter.Operator.LTE, DateUtil.parse(endDate,"yyyyMMddHHmmss"));
        page.addFilter("userID", SearchFilter.Operator.EQ,userID);
        page.addFilter("mobile", SearchFilter.Operator.EQ,mobile);
        page.addFilter("status", SearchFilter.Operator.EQ,status);

        page = orderService.queryPage(page);
        return Rets.success(page);
    }

    //微信端下单功能
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Object save(@RequestParam("mobile") String mobile,
                       @RequestParam("userID") String userID,
                       @RequestParam("remark") String remark,
                       @RequestParam("content") String content,
                       @RequestParam("price") BigDecimal price)
    {
//        Orderdish orderdish=Json.fromJson(Orderdish.class,content);
//        System.out.println("转化："+orderdish);
        JSONArray jsonArray=JSONArray.parseArray(content);
        for(int i=0;i<jsonArray.size();i++){
            JSONObject object1= (JSONObject) jsonArray.get(i);
                String name;
                int number;
                name=(String)object1.get("name");
                number=(Integer) object1.get("number");
            SearchFilter filter= new SearchFilter("title", SearchFilter.Operator.EQ,name);
                List<Dish> dishlist=dishService.queryAll(filter);
            System.out.println("订单内容str"+dishlist);
            Dish dish=dishlist.get(0);
            dish.setInventory(dish.getInventory()-number);
            dishService.update(dish);
        }
        Order order=new Order();
        order.setContent("{\"list\":"+content+"}");
        System.out.println("订单内容str"+content);
        order.setMobile(mobile);
        order.setRemark(remark);
        order.setStatus("备货中");
        order.setUserID(userID);
        order.setPrice(price);
        order.setCreateTime(new Date());
        orderService.insert(order);
        return Rets.success();
    }
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Object delete(@RequestParam long id){
        orderService.delete(id);
        return Rets.success();
    }
    //微信用户查看自己订单
    @RequestMapping(value = "/userlist",method = RequestMethod.GET)
    public Object userlist(@RequestParam String userID){
        SearchFilter filter= new SearchFilter("userID", SearchFilter.Operator.EQ,userID);
        return Rets.success(orderService.queryAll(filter));
    }

}
